-- 加载贴图，动画
local assets = {Asset("ANIM", "anim/zhubi.zip"), Asset("IMAGE", "images/zhubi.tex"), Asset("ATLAS", "images/zhubi.xml")}

local function zhubi_oneaten(inst, eater)
    -- DoTaskInTime is to let the eat animation finish, since we don't have a callback for that.
    eater:DoTaskInTime(0.5, function()
        if eater:IsValid() and
                not (eater.components.freezable ~= nil and eater.components.freezable:IsFrozen()) and
                not (eater.components.pinnable ~= nil and eater.components.pinnable:IsStuck()) and
                not (eater.components.fossilizable ~= nil and eater.components.fossilizable:IsFossilized()) then

            local sleeptime = TUNING.MOON_MUSHROOM_SLEEPTIME

            local mount = eater.components.rider ~= nil and eater.components.rider:GetMount() or nil
            if mount ~= nil then
                mount:PushEvent("ridersleep", { sleepiness = 4, sleeptime = sleeptime })
            end

            if eater.components.sleeper ~= nil then
                eater.components.sleeper:AddSleepiness(4, sleeptime)
            elseif eater.components.grogginess ~= nil then
                eater.components.talker:Say("吃完外卖就晚安咯")
                eater.components.grogginess:AddGrogginess(2, sleeptime)
            else
                eater:PushEvent("knockedout")
            end
        end
    end)
end

function fn()
    local assetname = "zhubi"

    local inst = CreateEntity() -- 创建实体
    inst.entity:AddTransform() -- 添加xyz形变对象
    inst.entity:AddAnimState() -- 添加动画状态
    inst.entity:AddNetwork() -- 添加这一行才能让所有客户端都能看到这个实体

    MakeInventoryPhysics(inst)

    inst.AnimState:SetBank(assetname) -- 地上动画
    inst.AnimState:SetBuild(assetname) -- 材质包，就是anim里的zip包
    inst.AnimState:PlayAnimation("idle") -- 默认播放哪个动画

    MakeInventoryFloatable(inst)
    --------------------------------------------------------------------------
    if not TheWorld.ismastersim then
        return inst
    end
    --------------------------------------------------------------------------
    inst:AddComponent("inspectable") -- 可检查组件
    inst:AddComponent("inventoryitem") -- 物品组件

    inst.components.inventoryitem.atlasname = "images/zhubi.xml" -- 在背包里的贴图

    inst:AddComponent("edible") -- 可食物组件
    inst.components.edible.foodtype = FOODTYPE.GOODIES
    inst.components.edible.hungervalue = 0
    inst.components.edible.healthvalue = 0
    inst.components.edible.sanityvalue = 1
    inst.components.edible:SetOnEatenFn(zhubi_oneaten)
    inst:AddComponent("stackable") -- 可堆叠
    inst.components.stackable.maxsize = TUNING.STACK_SIZE_SMALLITEM

    MakeHauntableLaunch(inst)

    return inst
end

return Prefab("zhubi", fn, assets, prefabs)